<?php

class TestDbQuery extends UnitTestCase {

    function testSelectSimple() {
        $q      = new DbQuery();
        $select = 'u.screen_name, u.email';
        $from   = 'users AS u';
        $where1 = "login='toto'";
        $order  = "date DESC";
        $limit  = "1";

        $this->assertTrue($q->setSelect($select), "setSelect($select) should return true");
        $this->assertTrue($q->setFrom($from), "setFrom($from) should return true");
        $this->assertTrue($q->addWhere($where1,'login'), "addWhere($where1,'login') should return true");
        $this->assertTrue($q->setOrder($order), "setOrder($order) should return true");
        $this->assertTrue($q->setLimit($limit), "setLimit($limit) should return true");


        // getting whole SQL
        $this->assertEqual(
            $q->getSql('select')
            , "SELECT u.screen_name, u.email FROM users AS u WHERE login='toto' ORDER BY date DESC LIMIT 1"
            ,"getSql('select') should return full select SQL query"
        );
    }

    function testWhere() {
        $q = new DbQuery();

        $q->addWhere("var1 = 'value1'",'var1');
        $q->addWhere("var2 = 36",'var2');
        $q->addWhere("var3 LIKE '%something%'", 'var3');

        $this->assertTrue(
            $q->removeWhere('var2')
            , "removeWhere(var2) should return true");
        $this->assertFalse(
            $q->removeWhere('var4')
            , "removeWhere(var4) should return false");
        $this->assertFalse(
            $q->addWhere(array(), 'var5')
            ,"addWhere(var4) should accept only strings");

        $this->assertEqual(
            $q->getWhere()
            , "var1 = 'value1' AND var3 LIKE '%something%'"
            , "getWhere() should return the WHERE statement AS SQL string, excluding removed clauses"
        );

        $this->assertTrue($q->flushWhere()
        , "flushWhere() should return true...");
        $this->assertEqual($q->getWhere(), ''
        , "...and then getWhere() should return an empty string.");
    }

    function testDelete() {
        $q = new DbQuery();

        $q->setFrom('users');
        $q->addWhere('id = 12');
        $q->setLimit('1');
        
        $this->assertEqual(
            $q->getSql('delete')
            ,"DELETE FROM users WHERE id = 12 LIMIT 1"
        );
    }

    /*function testInsert() {
        $q = new DbQuery();

        $q->setInsert();
    }*/
}